﻿namespace How_toEval;

public class Memory
{
    public Memory? EnclosingMemory { get; set; }

    Dictionary<string, object> KeyValuePairs { get; }

    public object Get(string name)
    {
        if (KeyValuePairs.TryGetValue(name, out var value))
        {
            return value;
        }
        else
        {
            throw new Exception($"Var {name} declared in the scope but did not assigned");
        }
    }

    public Memory(Dictionary<string, object> keyValuePairs, Memory? enclosingMemory = null)
    {
        EnclosingMemory = enclosingMemory;
        KeyValuePairs = keyValuePairs;
    }

    public void Set(string name, object res)
    {
        KeyValuePairs[name] = res;
    }
}
